package com.hyb.i18n.web;

import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Component;
import java.util.Locale;

/**
 * @Description: 国际化语音工具
 * @author: 黄兴杰
 * @date: 2021/7/31 11:21
 * @version: V1.0
 */
@Component
public class LanguageUtil extends ReloadableResourceBundleMessageSource {

    public LanguageUtil(){
        setDefaultLocale(Locale.CHINA);
        setDefaultEncoding("UTF-8");
        setBasenames("classpath:messages/messages","classpath:messages/web");
    }

    /**
     * 根据msgCode获取值
     * @author: HuangXingJie
     * @date: 2021/8/2 9:13
     * @param msgCode 消息code
     * @param languageTag 语言
     * @return:
     */
    public String getMessage(String msgCode,String languageTag){
        Locale locale = Locale.forLanguageTag(languageTag);
        return super.getMessage(msgCode,null,"error",locale);

    }

    /**
     * 根据msgCode获取值
     * @author: HuangXingJie
     * @date: 2021/8/2 9:13
     * @param msgCode 消息code
     * @param languageTag 语言
     * @return:
     */
    public String getMessage(String msgCode,String defaultMsg,String languageTag){
        Locale locale = Locale.forLanguageTag(languageTag);
        return super.getMessage(msgCode,null,defaultMsg,locale);

    }

    /**
     * 获取当前语音
     * @author: HuangXingJie
     * @date: 2021/8/2 9:12
     * @return:
     */
    public String getCurrentLanguage(){
        return super.getDefaultLocale().getLanguage();
    }

    /**
     * 设置语音
     * zh-CN 简体中文
     * zh-TW 繁体中文
     * fr-FR 法国法语
     * de-DE 德国
     * en    英语
     * en-GB 英国英语
     * en-UG 美国英语
     * @author: HuangXingJie
     * @date: 2021/7/31 13:27
     * @param languageTag
     * @return:
     */
    public void setLanguage(String languageTag){
        Locale locale = Locale.forLanguageTag(languageTag);
        super.setDefaultLocale(locale);
    }

    /**
     * 根据msgCode获取值
     * @author: HuangXingJie
     * @date: 2021/8/2 9:13
     * @param: msgCode  消息code
     * @param defaultMsg 默认消息
     * @return:
     */
    public String getMessageByCode(String msgCode,String defaultMsg){
        return super.getMessage(msgCode,null,defaultMsg,super.getDefaultLocale());
    }

    /**
     * 根据msgCode获取值
     * @author: HuangXingJie
     * @date: 2021/8/2 9:13
     * @param msgCode
     * @return:
     */
    public String getMessageByCode(String msgCode){

        return super.getMessage(msgCode,null,"error",super.getDefaultLocale());

    }


}
